/*******************************************************************
 * 外语翻译平台(translate) 
 *
 * 成都一亿信息技术公司 版权所有
 *
 * Copyright (C) 2010 Transer Information Technology Co., Ltd.
 *
 * @Version : 1.0 2010-12-29 上午11:01:15
 *
 * UserDaoImpl.java 
 *
 *
 *******************************************************************/
package com.transer.persistance.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.transer.persistance.bean.ProfileLanguage;
import com.transer.persistance.bean.User;
import com.transer.persistance.dao.IUserDao;
import com.transer.persistance.exception.TranserDatabaseException;
import com.transer.utils.Utils;
import com.transer.web.page.TranserPageData;

/**
 * 注意：事务的属性和基本概念
 * 
 * Required : 如果在一个事务中调用，就将该方法加到此事务中，如果没有启动事务，就创建新事务
 * 
 * RequiredNew ： 不管当前有没有事务，都启动新事务，如果有，会被挂起，直到此方法结束
 * 
 * NotSupported ：不能在事务中执行此方法，如果有事务，会被挂起，直到此方法结束
 * 
 * Supports ： 如果有当前事务，此方法回加到当前事务，如果没有，容器不会启动新事务
 * 
 * Mandatory ： 必须在事务中执行此方法，否则会抛出异常 ： TransactionRequiredException
 * 
 * Never ：必须不在事务中调用此方法，否则抛出RemoteException(远程调用)或EJBException(本地调用)
 * 
 * @author Administrator
 * 
 */
public class UserDaoImpl extends SqlMapClientDaoSupport implements IUserDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.transer.persistance.dao.IUserDao#deleteLanguagePair(int, int)
	 */
	@Override
	public void deleteLanguagePair(int userid, int id) throws TranserDatabaseException {
		try {
			ProfileLanguage p = new ProfileLanguage();
			p.setUserID(userid);
			p.setId(id);
			getSqlMapClientTemplate().delete("ProfileLanguage.deleteProfileLanguage", p);
		} catch (DataAccessException e) {
			throw new TranserDatabaseException("删除语言方向出错");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.transer.persistance.dao.IUserDao#addLanguagePair(com.transer.persistance
	 * .bean.ProfileLanguage)
	 */
	@Override
	public int addLanguagePair(ProfileLanguage pair) throws TranserDatabaseException {
		try {
			return (Integer) getSqlMapClientTemplate().insert("ProfileLanguage.addProfileLanguage", pair);
		} catch (DataAccessException e) {
			throw new TranserDatabaseException("添加语言方向出错");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.transer.persistance.dao.IUserDao#getProfileLanguages(int)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ProfileLanguage> getProfileLanguages(int userid) throws TranserDatabaseException {
		try {
			return getSqlMapClientTemplate().queryForList("ProfileLanguage.getParisByUserID", userid);
		} catch (DataAccessException e) {
			throw new TranserDatabaseException("获取语言方向出错");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.transer.persistance.dao.IUserDao#loginPassport(java.lang.String)
	 */
	@Override
	public User loginPassport(String email) throws TranserDatabaseException {
		return isEmailExsit(email);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.transer.persistance.dao.IUserDao#isEmailExsit(java.lang.String)
	 */
	public User isEmailExsit(final String email) throws TranserDatabaseException {
		try {
			// 参数
			Map<String, String> param = new HashMap<String, String>();
			param.put("EMAIL", email);
			return (User) getSqlMapClientTemplate().queryForObject("User.getUserByParam", param);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new TranserDatabaseException("find user by email error", e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.transer.persistance.dao.IUserDao#getUserByID(int)
	 */
	@Override
	public User getUserByID(int id) throws TranserDatabaseException {
		try {
			// 参数
			Map<String, Integer> param = new HashMap<String, Integer>();
			param.put("ID", id);
			User r = (User) getSqlMapClientTemplate().queryForObject("User.getUserByParam", param);
			return r;
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new TranserDatabaseException("find user by id error", e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.transer.persistance.dao.IUserDao#getUserByNikeName(java.lang.String)
	 */
	@Override
	public boolean isNikeNameExsit(String nikeName) throws TranserDatabaseException {
		try {
			// 参数
			Integer size = (Integer) getSqlMapClientTemplate().queryForObject("User.getNikenameSize", nikeName);
			size = Utils.getInteger(size);
			return size > 0;
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new TranserDatabaseException("find user by nikeName error", e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.transfer.persistance.dao.IUserDao#addUser(com.transfer.persistance
	 * .bean.passprot.User)
	 */
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, rollbackFor = { TranserDatabaseException.class })
	public void addUser(User user) throws TranserDatabaseException {
		try {
			SqlMapClientTemplate sqlMapClient = getSqlMapClientTemplate();
			// 保存用户
			sqlMapClient.insert("User.addUser", user);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new TranserDatabaseException("add user error", e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.transer.persistance.dao.IUserDao#updateUser(com.transer.persistance
	 * .bean.passport.User)
	 */
	@Override
	public void updateUser(User user) throws TranserDatabaseException {
		try {
			SqlMapClientTemplate sqlMapClient = getSqlMapClientTemplate();
			// 保存用户
			sqlMapClient.update("User.updateUser", user);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new TranserDatabaseException("update user error", e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.transer.persistance.dao.IUserDao#findUserSize(java.util.Map)
	 */
	@Override
	public int findUserSize(Map<String, Object> params) throws TranserDatabaseException {
		try {
			return (Integer) getSqlMapClientTemplate().queryForObject("User.findUserSize", params);
		} catch (DataAccessException e) {
			throw new TranserDatabaseException("查找用户大小 出错");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.transer.persistance.dao.IUserDao#findUsers(java.util.Map, int,
	 * int)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<TranserPageData> findUsers(final Map<String, Object> params, final int skipResults, final int maxResults) throws TranserDatabaseException {
		try {
			return getSqlMapClientTemplate().queryForList("User.findUser", params, skipResults, maxResults);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new TranserDatabaseException("findUsers error", e);
		}
	}
}
